public class Solution670 {
    public int maximumSwap(int num) {
        char[] chars=String.valueOf(num).toCharArray();
        int l=0;
        int target=-1;
        while (l<chars.length){
            char max=chars[l];
            target=l;
            for (int i=l+1;i<chars.length;i++){
                if (chars[i]>max || chars[i]==max && target!=l){
                    max=chars[i];
                    target=i;
                }
            }
            if (target==l){
                l++;
            }else {
                char t=chars[target];
                chars[target]=chars[l];
                chars[l]=t;
                return Integer.parseInt(String.valueOf(chars));
            }
        }
        return num;
    }

    public static void main(String[] args) {
        System.out.println(new Solution670().maximumSwap(9973));
    }
}
